<?php 
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class IndexController extends Zend_Controller_Action
{

    public function init(){
		/*$registry = Zend_Registry::getInstance();
		$this->_db = $registry->get('db');	
		Zend_Loader::loadFile('../application/default/models/Common.php');
    	$common_model = new Common();
    	if(isset(Zend_Auth::getInstance()->getStorage()->read()->userid)) {
			$user_theme = $common_model->getUserThemeById(Zend_Auth::getInstance()->getStorage()->read()->userid);
			$this->view->user_theme = $user_theme;
    	}*/	
	}
    
	public function indexAction(){
		$this->_helper->layout->setLayout('index1_layout');
		$storage = new Zend_Auth_Storage_Session();
        $ses = $storage->read();
		$this->view->register_id = $ses->reg_id;
		
	 }

    public function mainAction(){
		$this->_helper->layout->setLayout('index_layout');
		Zend_Loader::loadFile("../application/default/models/Media.php");
		Zend_Loader::loadFile("../application/admin/models/News.php");
		Zend_Loader::loadFile("../application/default/models/News.php");
    	$modNews = new News();
    	$modFrontNews = new FrontNews();
    	$default_news_arr = $modNews->getMainDetails();
    	$news_arr = $modFrontNews->listNews('news1');
    	$news_arr2 = $modFrontNews->listNews('news2');
    	
    	$this->view->default_news_arr = $default_news_arr;
    	$this->view->news_arr = $news_arr;
    	$this->view->news_arr2 = $news_arr2;
    	
		/*$storage = new Zend_Auth_Storage_Session();
        $ses = $storage->read();
		$this->view->register_id = $ses->reg_id;*/
	 }
    /**
     * Gets the adapter for authentication against a database table
     *
     * @return object
     */
    protected function getAuthAdapter()
    {
    	//Zend_Loader::loadFile('Zend/Auth/Adapter/DbTable.php');
        //$dbAdapter = Zend_Registry::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($this->_db);

        $authAdapter->setTableName('user')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password')
                    ->setCredentialTreatment('(?)');
                    
        return $authAdapter;
    }

    /**
     * Create and return the login form
     *
     * @return object
     */
    protected function getLoginForm()
    {
    	$nickname = new Zend_Form_Element_Text('username');
        $nickname->setLabel('Nickname:')
                ->setRequired(true);

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
                ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('login');
        $submit->setLabel('Login');

        $loginForm = new Zend_Form();
        $loginForm->setAction($this->_request->getBaseUrl().'/login/index/')
                ->setMethod('post')
                ->addElement($nickname)
                ->addElement($password)
                ->addElement($submit);

        return $loginForm;
    }
    public function forbiddenAction(){
    	
    }
  
  public function adminviewAction()
  {
  	$this->_helper->layout->setLayout('layout');  	
  	Zend_Loader::loadFile('../application/default/models/User.php');
  	
  	if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $this->_redirect('default');
    }
  	
  	$controller = $this->getFrontController()->getDefaultControllerName();
  	$user_model = new User();
  	$userInfo = Zend_Auth::getInstance()->getStorage()->read();
	$this->view->first_name = $userInfo->first_name;
  	$this->view->controller = $controller;
  	
  	$user_stat_arr = $user_model->getUserStats();
  	$mem_count = $user_model->getMemberCount();
  	$live_users = $user_model->getLiveUsers();
  	$this->view->user_stat_arr = $user_stat_arr;
  	$this->view->mem_count = $mem_count;
  	$this->view->live_users = $live_users;
  }
  
  public function userviewAction()
  {
  	//$this->_helper->layout->setLayout('user');
  	//$this->_helper->layout()->disableLayout();
  	Zend_Loader::loadFile('../application/default/models/User.php');
  	
  	if(!Zend_Auth::getInstance()->hasIdentity())
    {
        $this->_redirect('default');
    }
  	
  	$controller = $this->getFrontController()->getDefaultControllerName();  	
  	$user_model = new User();
  	$userInfo = Zend_Auth::getInstance()->getStorage()->read();
  	$userlevel = Zend_Auth::getInstance()->getStorage()->read()->userlevel;
	$this->view->first_name = $userInfo->first_name;
	$this->view->last_name = $userInfo->last_name;
	$this->view->email = $userInfo->email_address;
	$this->view->userid = $userInfo->userid;
	$this->view->userlevel = $userlevel;	
  	$this->view->controller = $controller;  	
  	
  	switch($userlevel)
  	{
  		case "bde" :
  			$user_role = "Business Development Executive";
  			break;
  		
  		case "bse" :
  			$user_role = "Business Solution Executive";
  			break;
  			
  		case "pl" :
  			$user_role = "Project Leader";
  			break;
  			
  		case "dev" :
  			$user_role = "Developer";
  			break;
  	}
  	
  	$getProfile_image = $user_model->getProfileImage($userInfo->userid);
  	
  	$this->view->user_role = $user_role;
  	$this->view->profile_img = $getProfile_image;
  	
  	$customer_info = $user_model->getUserCustomerInfoByUserId($userInfo->userid);
  	$project_leader_info = $user_model->getProjectLeader($userInfo->userid);
  	$group_mem_arr = $user_model->getPLGroupMembers($userInfo->userid);
  	$this->view->customer_info = $customer_info;
  	$this->view->project_leader_info = $project_leader_info;
  	$this->view->group_mem_arr = $group_mem_arr;
  }
  
	public function resetpasswdAction()
	{
		Zend_Loader::loadFile('../application/default/models/User.php');   	
		$user_model = new User();
		$request = $this->getRequest();
		$username = $request->getParam('username');  
		$reset_password = $user_model->resetPassword($username);
		exit;
	}
  
	public function loadpageAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$request = $this->getRequest();
		$url = $request->getParam('url');         
		
		$mod = explode('/', $url);
		$module = $mod[2];
		
		switch ($module) {        
		    case "user" :
		        $this->_helper->actionStack('index', 'user');
		        break;
		        
		    case "customer" :
		        $this->_helper->actionStack('index', 'customer');
		        break;
		        
		    case "project" :
		        $this->_helper->actionStack('index', 'project');
		        break;
		        
		    case "discussion" :
		        $this->_helper->actionStack('index', 'discussion');
		        break;    
		        
		    default :
		        $this->_helper->actionStack('index', 'index');
		        break;    
		}
	//exit;    
	}
	
	public function loadfrontpageAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$request = $this->getRequest();
		$url = $request->getParam('url');         
		
		$mod = explode('/', $url);
		$module = $mod[1];
		$action = $mod[2];		
		
		switch ($action) {        
		    case "editprofile" :
		        $this->_helper->actionStack('editprofile', 'user');
		        break;
		        
		    case "index" :
		        $this->_helper->actionStack('index', 'event');
		        break;    			
		}    
	}

	public function previewprofileimgAction()
	{
		$this->_helper->layout()->disableLayout();
		$request = $this->getRequest();		
		$filename = $request->getParam('filename');		
		$profile_img = "images/profileimg/large/" . $filename;
		
		list($width, $height, $type, $attr) = getimagesize($profile_img);
		$this->view->filename = $filename;
		$this->view->width = $width;
		$this->view->height = $height;
	}
	
	public function getlocationAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/default/models/Common.php');
		
		$common_model = new Common();
		$request = $this->getRequest();
		$cust_id = $request->getParam('cust_id');
		$loc_arr = $common_model->getLocationByCustId($cust_id);
		
		$this->view->loc_arr = $loc_arr;		
	}
}
?>
